<?php

class weixinapi{

    private $appid,$appkey,$callback,$access_token,$openid;

    public function __construct($appid, $appkey, $callback){
        $this->appid = $appid;
        $this->appkey = $appkey;
        $this->callback = $callback;
        $this->access_token= '';
        $this->openid = '';
    }

    public function redirect_to_login() {
        
        //跳转到微信登录页的接口地址, 不要更改!!
        //$redirect = "https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=$this->appid&scope=&redirect_uri=".rawurlencode($this->callback);
        
        $redirect = "https://open.weixin.qq.com/connect/qrconnect?appid=$this->appid&redirect_uri={$this->callback}&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect";
        header("Location:$redirect");
    }


    //获得登录的 openid
    public function get_openid($code){
        //$url = "https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=$this->appid&client_secret=$this->appkey&code=$code&redirect_uri=".rawurlencode($this->callback);
        $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->appid}&secret={$this->appkey}&code={$code}&grant_type=authorization_code";
        $content = $this->vGet($url);
        $arr = json_decode($content);
        if (!empty($arr->refresh_token)) {
            $this->refresh_token=$arr->refresh_token;
            $url = "https://api.weixin.qq.com/sns/oauth2/refresh_token?appid={$this->appid}&grant_type=refresh_token&refresh_token={$this->refresh_token}";
            $content = $this->vGet($url);
            $returns = json_decode($content);
            if (!empty($returns->openid)) {
                return $returns->openid;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
    * 返回用户信息
    * 
    */
    public function get_user_info(){
        $url = "https://graph.qq.com/user/get_user_info?access_token=$this->access_token&oauth_consumer_key=$this->appid&openid=$this->openid";
        $content=file_get_contents($url);
        $result = json_decode($content);
        return $result->nickname;
    }
    public function vGet($url){ // 模拟提交数据函数
        $curl = curl_init(); // 启动一个CURL会话
        curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); // 对认证证书来源的检查
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); // 从证书中检查SSL加密算法是否存在
        curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); // 模拟用户使用的浏览器
        // curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
        // curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
        curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
        $tmpInfo = curl_exec($curl); // 执行操作
        if (curl_errno($curl)) {
           echo 'Errno'.curl_error($curl);//捕抓异常
        }
        curl_close($curl); // 关闭CURL会话
        return $tmpInfo; // 返回数据
    }
}
?>